<?php

//*******************************************************
//* JAccelerator Component
//* http://fun.kubera.org
//* (C) Radoslaw Kubera
//* license http://www.gnu.org/copyleft/gpl.html GNU/GPL
//*******************************************************

defined('_VALID_MOS') or die();

DEFINE('_JOOMLACACHE_VERSION', '1.1.7');

function JoomlaCacheConvertLink($url_joomlacache) {
	if($url_joomlacache == '/')
		$url_joomlacache = '/index.php';
	return md5($url_joomlacache);

}

function JoomlaCacheCheckFlushRemove() {
	$time = time();
	$update_status = 0;
	$today = date('d');
	$hour = date('H');

	$flush_after_hour_flag = '';
	$flush_after_hours_flag = 0;
	$remove_after_hour_flag = '';
	$remove_after_hours_flag = 0;

	global $mosConfig_absolute_path;

	$path = $mosConfig_absolute_path.'/components/com_joomlacache/';
	if(file_exists($path.'config.php')) {
		include ($path.'config.php');
		if(file_exists($path.'status.php')) {
			include ($path.'status.php');
			$today_flush = $flush_after_hour_flag;
			$time_flush = $flush_after_hours_flag;
			$today_remove = $remove_after_hour_flag;
			$time_remove = $remove_after_hours_flag;
		} else {
			$today_flush = date('d');
			$time_flush = time();
			$today_remove = date('d');
			$time_remove = time();

			$update_status = 1;
		}

		if(isset($params['remove_after_hour']) && $today != $remove_after_hour_flag) {
			if(isset($params['remove_after_hour_option']) && $hour >= $params['remove_after_hour_option']) {
				$update_status = 1;
				JoomlaCacheRemove();
				JoomlaCacheRemoveModules();
				$today_remove = date('d');
			}
		}

		if(isset($params['remove_after_hours'])) {
			$after_time = $remove_after_hours_flag + ($params['remove_after_hours_option'] * 3600);
			if($after_time <= $time) {
				$update_status = 1;
				JoomlaCacheRemove();
				JoomlaCacheRemoveModules();
				$time_remove = $time;
			}
		}

		if(isset($params['flush_after_hour']) && $today != $flush_after_hour_flag) {
			if(isset($params['flush_after_hour_option']) && $hour >= $params['flush_after_hour_option']) {
				$update_status = 1;
				JoomlaCacheFlush();
				JoomlaCacheFlushModules();
				$today_flush = date('d');

			}
		}

		if(isset($params['flush_after_hours'])) {
			$after_time = $flush_after_hours_flag + ($params['flush_after_hours_option'] * 3600);
			if($after_time <= $time) {
				$update_status = 1;
				JoomlaCacheFlush();
				JoomlaCacheFlushModules();
				$time_flush = $time;
			}
		}

		if($update_status == 1) {
			$txt = "<?php\n";
			$txt .= '$'.'flush_after_hour_flag ='.$today_flush.";\n";
			$txt .= '$'.'flush_after_hours_flag ='.$time_flush.";\n";
			$txt .= '$'.'remove_after_hour_flag ='.$today_remove.";\n";
			$txt .= '$'.'remove_after_hours_flag ='.$time_remove.";\n";
			$txt .= "?>";
			ob_start();
			$file = fopen($path.'status.php', "w+");
			fwrite($file, $txt);
			fclose($file);
			ob_end_clean();
		}
	} else{
		return;
	}
}

function JoomlaCacheBaypass(&$time, &$type) {
	if(eregi('rss', strtolower($_SERVER['REQUEST_URI'])) && eregi('index2', strtolower($_SERVER['REQUEST_URI']))) {
		return 0;
	} else
		if(eregi('index2', strtolower($_SERVER['REQUEST_URI'])))
			return 1;

	if(strtolower($_SERVER['REQUEST_METHOD']) != 'get')
		return 1;

	$ret = 1;
	global $option, $mosConfig_absolute_path;

	$path = $mosConfig_absolute_path.'/components/com_joomlacache/components.php';
	if(file_exists($path))
		include $path;

	if(isset($$option)) {
		if($$option->cache != 0)
			$ret = 0;
		$time = $$option->time;
		$type = $$option->cache;
	}
	return $ret;
}

function JoomlaCacheClear($components) {
	ob_start();
	global $mosConfig_absolute_path;
	$comparray = array();
	foreach ($components as $component) {
		$comparray[] = $component->option;
	}
	$path_base = $mosConfig_absolute_path.'/components/com_joomlacache/cache/components/';
	$dir = opendir($path_base);
	while($file = readdir($dir)) {
		if($file != '.' && $file != '..' && $file != 'com_content') {
			if(!in_array($file, $comparray)) {
				JoomlaCacheDeleteDirectory($path_base.$file);
				JommlaCacheDeleteFromDB($file);
			}
		}
	}
	closedir($dir);
	ob_end_clean();
}

function JoomlaCacheClearModules($modules) {
	ob_start();
	global $mosConfig_absolute_path;
	$modarray = array();
	foreach ($modules as $module) {
		$modarray[] = $module->id;
	}
	$path_base = $mosConfig_absolute_path.'/components/com_joomlacache/cache/modules/';
	$dir = opendir($path_base);
	while($file = readdir($dir)) {
		if($file != '.' && $file != '..') {
			if(!in_array($file, $modarray)) {
				JoomlaCacheDeleteDirectory($path_base.$file);
				JommlaCacheDeleteFromDB($file);

			}
		}
	}
	closedir($dir);
	ob_end_clean();
}

function JoomlaCacheFlush($component = null) {
	ob_start();
	global $mosConfig_absolute_path, $database;

	$database->setQuery("select * from #__components where link<>''");
	$components = $database->loadObjectList();
	JoomlaCacheClear($components);

	if(file_exists($mosConfig_absolute_path.'/components/com_joomlacache/components.php')){
		include $mosConfig_absolute_path.'/components/com_joomlacache/components.php';
	}

	if(!$component) {
		JoomlaCacheFlush('com_content');

		foreach ($components as $comp) {
			JoomlaCacheFlush($comp->option);
		}
	} else {
		if(isset($$component)) {
			$type = $$component->cache;
			$time = $$component->time;
			$path = $mosConfig_absolute_path.'/components/com_joomlacache/cache/components/'.$component.'/';

			$path_base = $mosConfig_absolute_path.'/components/com_joomlacache/cache/components/'.$component.'/files/';
			if(is_dir($path_base)) {
				$clear = 1;

				$end_time = mktime() - $time;
				$end_time = date("Y-m-d H:i:s", $end_time);

				$database->setQuery("select filename from #__joomlacache where element='$component' and date<'$end_time'");
				$files = $database->loadObjectList();
				foreach ($files as $file) {
					@unlink($path_base.$file);
				}
				$database->setQuery("delete from #__joomlacache where element='$component'");
				$database->query();
			}
		}
	}
	ob_end_clean();
}

function JoomlaCacheFlushModules($module = null) {
	ob_start();
	global $mosConfig_absolute_path, $database;

	$database->setQuery("select * from #__modules where access<'99'");
	$modules = $database->loadObjectList();
	JoomlaCacheClearModules($modules);

	if(file_exists($mosConfig_absolute_path.'/components/com_joomlacache/modules.php'));
	include $mosConfig_absolute_path.'/components/com_joomlacache/modules.php';

	if(!$module) {
		foreach ($modules as $mod) {
			JoomlaCacheFlush($mod->id);
		}
	} else {
		$mod = 'id_'.$module;
		if(isset($$mod)) {
			$type = $$mod->cache;
			$time = $$mod->time;
			$path = $mosConfig_absolute_path.'/components/com_joomlacache/cache/modules/'.$module.'/';

			$path_base = $mosConfig_absolute_path.'/components/com_joomlacache/cache/modules/'.$module.'/files/';
			if(is_dir($path_base)) {
				$clear = 1;
				$end_time = mktime() - $time;
				$end_time = date("Y-m-d H:i:s", $end_time);

				$database->setQuery("select date from #__joomlacache where element='$module' and date<'$end_time'");
				$files = $database->loadObjectList();
				foreach ($files as $file) {
					@unlink($path_base.$file);
					$database->setQuery("delete from #__joomlacache where element='$module'");
					$database->query();
				}
			}
		}
	}
	ob_end_clean();
}

function JoomlaCacheRemove($component = null) {
	ob_start();
	global $mosConfig_absolute_path, $database;

	$database->setQuery("select * from #__components where link<>''");
	$components = $database->loadObjectList();
	if($component)
		JoomlaCacheClear($components);

	if(!$component) {
		$path_base = $mosConfig_absolute_path.'/components/com_joomlacache/cache/components/';
		JoomlaCacheDeleteDirectory($path_base, true);
		JommlaCacheDeleteFromDB('allcomponents');
	} else {
		$path_base = $mosConfig_absolute_path.'/components/com_joomlacache/cache/components/'.$component.'/';
		JoomlaCacheDeleteDirectory($path_base, true);
		JommlaCacheDeleteFromDB($component);
	}
	ob_end_clean();
}

function JoomlaCacheRemoveModules($module = null) {
	ob_start();
	global $mosConfig_absolute_path, $database;

	$database->setQuery("select * from #__modules where access<'99'");
	$modules = $database->loadObjectList();
	if($module)
		JoomlaCacheClearModules($modules);

	if(!$module) {
		$path_base = $mosConfig_absolute_path.'/components/com_joomlacache/cache/modules/';
		JoomlaCacheDeleteDirectory($path_base, true);
		JommlaCacheDeleteFromDB('allmodules');

	} else {
		$path_base = $mosConfig_absolute_path.'/components/com_joomlacache/cache/modules/'.$module.'/';
		JoomlaCacheDeleteDirectory($path_base, true);
		JommlaCacheDeleteFromDB($module);

	}
	ob_end_clean();
}
function JommlaCacheDeleteFromDB($from) {
	global $database;
	if($from != 'allcomponents' && $from != 'allmodules') {
		$database->setQuery("delete from #__joomlacache where element='$from'");
		$database->query();
	} else
		if($from == 'allcomponents') {
			$database->setQuery("delete from #__joomlacache where component='1'");
			$database->query();
		} else {
			$database->setQuery("delete from #__joomlacache where component='0'");
			$database->query();
		}
}

function JoomlaCacheDeleteDirectory($dirname, $only_empty = false) {
	ob_start();
	if(!is_dir($dirname))
		return false;

	$dscan = array(realpath($dirname));
	$darr = array();
	while(!empty($dscan)) {
		$dcur = array_pop($dscan);
		$darr[] = $dcur;
		if($d = opendir($dcur)) {
			while($f = readdir($d)) {
				if($f == '.' || $f == '..')
					continue;
				$f = $dcur.'/'.$f;
				if(is_dir($f))
					$dscan[] = $f;
				else
					@unlink($f);
			}
			closedir($d);
		}
	}
	$i_until = ($only_empty)?1 : 0;
	for ($i = count($darr) - 1; $i >= $i_until; $i--) {
		@rmdir($darr[$i]);
	}
	ob_end_clean();
}


?>
